﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.MetaAttribute;
using Csla.Wpf;
using OpenExpressApp.Command;
using Csla.Core;
using System.ComponentModel;
using System.Windows;
using OpenExpressApp.Template;
using OpenExpressApp.Types;
using Csla;
using OpenExpressApp.WPF.Controls;
using System.Collections.ObjectModel;
using log4net;
using System.Collections;
using OpenExpressApp.MetaModel;
using Avalon.Windows.Controls;
using OpenExpressApp.WPF.Templete;
using OpenExpressApp.Module;
using OpenExpressApp.Module.WPF;
using System.Windows.Controls;
using Ricciolo.Controls;
using System.Reflection;
using System.Windows.Markup;
using System.Diagnostics;
using AvalonDock;
using OpenExpressApp.Module.WPF.Editors;
using Csla.OpenExpressApp;

namespace OpenExpressApp.WPF.Command
{

    [Command(CommandNames.AddChildTable, Label = "添加子表记录", ToolTip = "添加子表记录")]
    public class AddChildTableCommand : WPFListViewCommand
    {
        public override bool CanExecute(ListObjectView view)
        {
            bool result = false;
            var bindingList = view.Data as IBindingList;

            if (bindingList != null && bindingList.AllowNew && view.CurrentObject != null)
            {
                var currentNode = view.CurrentObject as ITreeNode;
                var list = view.Data as IBindingList;
                var treeList = list as ITreeNodeCollection;

                //是否在处理树
                result = (treeList != null && currentNode != null && currentNode.ChildrenNodes.Count.Equals(0));   //是树 && 末级节点.
            }

            return result;
        }
        public override void Execute(ListObjectView view)
        {
            var cNode = view.CurrentObject;

           // view.BOType

            BusinessObjectInfo cBOInfo = ApplicationModel.GetBusinessObjectInfo(cNode.GetType());

            string sChildTableName = cBOInfo.TreeChildPropertyInfo.Name;

            var list = cNode.GetPropertyValue(sChildTableName) as IBindingList;

            BusinessBase newObject = null;
            
            if (newObject == null)
            {
                newObject = list.AddNew() as BusinessBase;
            }

            InitProperties(newObject, view);

            view.CurrentObject = newObject;
        }

        /// <summary>
        /// 设置某些属性
        /// 
        /// 添加记录时把导航关联值加入到当前记录中
        /// </summary>
        public void InitProperties(BusinessBase newObject, ListObjectView view)
        {
            var navigateView = view.NavigateQueryView;
            if (navigateView != null)
            {
                navigateView.SetReferenceEntity(newObject);
            }

            //设置父对象
            //设置foreignKey
            var parentKeyProp = view.GetChildForeignKey(newObject.GetType());
            if (string.IsNullOrEmpty(parentKeyProp) == false)
            {
                newObject.SetPropertyValue(parentKeyProp, view.CurrentID);
            }

            //设置foreignObject
            var parentEntityProp = view.GetChildForeignParent(newObject.GetType());
            if (string.IsNullOrEmpty(parentEntityProp) == false)
            {
                newObject.SetPropertyValue(parentEntityProp, view.CurrentObject);
            }
        }
    }
}